package com.example.project3.controller;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.example.project3.entity.Tasks;
import com.example.project3.entity.User;
import com.example.project3.entity.Worker;
import com.example.project3.service.impl.TasksServiceImpl;
import com.example.project3.service.impl.WorkerServiceImpl;
import com.example.project3.util.API;
import com.example.project3.util.MyJWT;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/task")
@Slf4j
public class TasksController {


    private TasksServiceImpl tasksServiceImpl;
    private WorkerServiceImpl workerServiceImpl;
    private com.example.project3.service.ICS.ICSServiceImpl ICSServiceImpl;
    private MyJWT myJWT;

    //获取项目里所有任务的方法
    @RequestMapping(value = "/{projectId}", method = RequestMethod.GET)
    public API getProjectTasks(@PathVariable Integer projectId) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("project_id", projectId);
        return API.success(tasksServiceImpl.listByMap(map));
    }


    //获取用户所有的任务
    @RequestMapping("/my")
    public API getUserTasks(HttpServletRequest request) {
        int uid = myJWT.getUid(request);
        return API.success(tasksServiceImpl.listUser(uid));
    }

    //增加任务的方法
    @RequestMapping("/add")
    public API addTask(HttpServletRequest httpServletRequest) throws IOException {
        ServletInputStream inputStream = httpServletRequest.getInputStream();
        InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
        StringBuilder stringBuilder = new StringBuilder();
        int available = inputStream.available();
        char[] chars = new char[available];
        int len = 0;
        while ((len = inputStreamReader.read(chars)) != -1) {
            stringBuilder.append(chars);
        }
        HashMap bean = JSONUtil.toBean(stringBuilder.toString(), HashMap.class);
        Tasks tasks = BeanUtil.copyProperties(bean, Tasks.class);
        String workers = bean.get("workers").toString();
        JSONArray jsonArray = new JSONArray(workers);
        List<Worker> list = new ArrayList<>();
        jsonArray.forEach(j -> {
            JSONObject jsonObject = (JSONObject) j;
            Integer uid = Integer.parseInt(String.valueOf(jsonObject.get("uid")));
            Worker worker = new Worker();
            worker.setUid(uid);
            list.add(worker);
        });
        log.info(list.toString());
        tasks.setPriority(tasks.getPriority()<0||tasks.getPriority()>3?0: tasks.getPriority());
        tasks.setStatus(tasks.getStatus()<0||tasks.getStatus()>3?0: tasks.getStatus());
        boolean save = tasksServiceImpl.save(tasks);
        User temp;
        for (Worker w : list) {
            w.setTaskId(tasks.getTaskId());
            temp = new User();
            temp.setUid(w.getUid());
            ICSServiceImpl.addEvent(temp, tasks);
        }
        boolean b = workerServiceImpl.saveBatch(list);
        return API.success(tasks);
    }


    //修改某个任务
    @RequestMapping("/update")
    public API updateTask(@RequestBody Tasks task) {
        tasksServiceImpl.updateById(task);
        return API.success();
    }


    @Autowired
    public void setTasksServiceImpl(TasksServiceImpl tasksServiceImpl) {
        this.tasksServiceImpl = tasksServiceImpl;
    }

    @Autowired
    public void setWorkerServiceImpl(WorkerServiceImpl workerServiceImpl) {
        this.workerServiceImpl = workerServiceImpl;
    }

    @Autowired
    public void setICSServiceImpl(com.example.project3.service.ICS.ICSServiceImpl icsServiceImpl) {
        this.ICSServiceImpl = icsServiceImpl;
    }

    @Autowired
    public void setMyJWT(MyJWT myJWT) {
        this.myJWT = myJWT;
    }
}
